import {SceneUtil} from "@/assets/js/scene/sceneUtil.js"
export default class SceneService {

  /**
   *  构造函数
   * @param mapBox HgcMapBoxUtil
   */
  constructor(mapBox) {
    this.hgcMapBoxUtil = mapBox; //地图操作基础类
    this.isMapInitComplete = false; //地图style初始化是否完成
    this.baseMapLayers = []; //当前显示的底图数组
    this.topCanvasLayers = []; //顶部不动的图层（标绘图层）
  }

  /**
   *  添加图层
   * @param type
   * @param id    layerId的真实身份是sourceId
   * @param url
   * @param minZoom
   * @param maxZoom
   * @param beforeID
   * @returns {*}
   */
  addLayer(layer, beforeID) {
    const realUrl = this._parseLayerUrl(layer.url);
    const param = this._parseLayerUrlParam(layer.url); //layerType,searchLayer
    console.log(realUrl)
    let promise;
    switch (param.layerType.trim()) {
      case "layer_sm_vec": //超图发布的矢量瓦片服务
        promise = this.hgcMapBoxUtil.addVectorTileLayer(layer.param ? layer.param.id : undefined, realUrl, beforeID);
        break;
      case "layer_sm_map": //栅格瓦片服务
        promise = this.hgcMapBoxUtil.addGridTileLayer(layer.param ? layer.param.id : undefined, realUrl, layer.minZoom, layer.maxZoom, beforeID);
        break;
      case "layer_sm_25d": //2.5D服务
        promise = this.hgcMapBoxUtil.addFillExtrusionLayer(layer.param ? layer.param.id : undefined, realUrl, layer.minZoom, layer.maxZoom, beforeID); //TODO  undefined 是sourcelayer
        break;
      case 'layer_tdt_map': //天地图栅格瓦片服务
        promise = this.hgcMapBoxUtil.addTdtGridTileLayer(layer.param ? layer.param.id : undefined, realUrl, layer.minZoom, layer.maxZoom, beforeID);
        break;
      case 'layer_sm_es': //EChart服务
        promise = this.hgcMapBoxUtil.addEchartsLayer(layer.url);
        break;
      default:
        break;
    }
    return promise;
  }


  /**
   * 获取专题最下面的图层
   * @returns {undefined}
   */
  getSubjectLayersBottomLayerId() {
    var layers = this.getMap().getStyle().layers;
    if (layers && Array.isArray(layers) && layers.length > 0) {
      return layers.length > 0 ? layers[0].id : undefined;
    }
    return undefined;
  }


  /**
   * 设置图层透明度
   * @param value
   * @param sourceId 数据源ID
   */
  setOpacityValue(value, layer) {
    if (layer.param && layer.param.id) {
      this.hgcMapBoxUtil.setLayerOpacity(layer.param.id, value);

    }

  }

  /**
   * 移除瓦片
   * @param soureId 数据源ID
   */
  removeLayer(sourceId) {
    this.hgcMapBoxUtil.removeTileLayer(sourceId);
  }

  /**
   * 切换底图
   * @param layerGroup
   */
  addBaseLayer(layerGroup) {
    if (!Array.isArray(layerGroup)) {
      throw ('layerGroup 不是个数组，检查数据！');
    }
    //移除
    this.baseMapLayers.forEach(layer => {
      this.removeLayer(layer.source);
    });
    this.baseMapLayers = [];
    //这里剩下专题图层
    var bottom = this.getSubjectLayersBottomLayerId();
    if (!bottom && this.topCanvasLayers.length > 0) {
      bottom = this.topCanvasLayers[0]; //TODO 有可能是最后一个
    }
    //排序再添加
    layerGroup.sort((p, n) => parseInt(n.index) - parseInt(p.index));
    layerGroup.forEach(layer => {
      this.addLayer(layer, bottom).then(res => {
        layer.param = res;
        this.baseMapLayers.push(res);
      });

    })
  }


  /**
   * 属性查询
   * @param layer
   * @param startIndex
   * @param size
   * @returns {Promise<never> | Promise<any>}
   */
  queryLayerAttribute(layer, startIndex, size,sql) {
    if (!layer) {
      return Promise.reject('图层不存在!');
    }
    const realUrl = this._parseLayerQueryUrl(layer.url);
    //const param = this._parseLayerUrlParam(layer.url);
    return SmQuery.mapServiceLayerSearchBySQL(layer.param ? layer.param.id : '', sql, realUrl, startIndex, size);
  }





}
